package com.example.chapter03.button;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.chapter03.R;
import com.example.chapter03.utils.TimeUtils;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class ClickButtonActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_btn_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_click_button);
        tv_btn_result = findViewById(R.id.tv_btn_result);
        Button btn_click_single = findViewById(R.id.btn_click_single);
        // 设置按键监听
        btn_click_single.setOnClickListener(new MyOnClickListener(tv_btn_result));
        // 设置公共点击监听器
        Button btn_click_public = findViewById(R.id.btn_click_public);
        btn_click_public.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_click_public) {
            String nowTime = TimeUtils.getNowTime();
            tv_btn_result.setText(nowTime);
        }
    }

    static class MyOnClickListener implements View.OnClickListener {
        private TextView tv_btn_result;
        public MyOnClickListener(View v) {
            this.tv_btn_result = (TextView) v;
        }
        @Override
        public void onClick(View v) {
            String nowTime = TimeUtils.getNowTime();
            tv_btn_result.setText(nowTime);
        }
    }
}